<?php 
namespace Admin\Controller;

class JueseController extends AdminController{
	private $_model = null; //数据库操作类
	private $_quanxian = null; //节点表数据库对象
	private $_juese_quanxian = null; //角色——权限表数据库对象
	private $_user_juese = null; //用户--角色表数据库对象

	//初始化操作
	public function _initialize(){
		parent::_initialize();
		$this->_model = M('juese');
		$this->_quanxian = M('quanxian');
		$this->_juese_quanxian = M("jq");
		$this->_user_juese = M("uj");
	}

	//列表详情
	public function index(){
		//查询数据
		$list = $this->_model->select();

		$arr = array();
		//遍历添加上 权限信息
		foreach($list as $v){
			$quanxians = $this->_juese_quanxian->where(array('jid'=>array('eq',$v['id'])))->select();
			$quanxian = array();
			foreach($quanxians as $value){
				$quanxian[] = $this->_quanxian->where(array('id'=>array('eq',$value['qid'])))->getField('name');
			}
			$v['quanxian'] = $quanxian;
			$arr[] = $v;
		}

	
		//分配变量
		$this->assign("list",$arr);
		//加载模板
		$this->display();                                
	}

	//执行添加操作
	public function doadd(){

		if(!$this->_model->create()){
			$this->error($this->_model->getError());
			exit;
		}

		if($this->_model->add() > 0){
			$this->success("添加成功！",U('juese/index'));
		}else{
			$this->error("添加失败！");
		}
	}


	//删除操作
	public function del(){
		// echo '<pre>';print_r($vo);echo '</pre>';exit;
		if($this->_model->delete($_GET['id']) > 0){
			$this->_user_juese->where(array('jid'=>$_GET['id']))->delete();
			$this->_juese_quanxian->where(array('jid'=>array('eq',$_GET['id'])))->delete();
			$this->success("删除成功！",U('juese/index'));
		}else{
			$this->error("删除失败");
		}
	}

	//加载修改页面c 
	public function edit(){
		//查出数据
		$vo = $this->_model->where(array('id'=>array('eq',I('id'))))->find();
		//向模板分配数据
		$this->assign('vo',$vo);
		//加载模板
		$this->display();
	}

	//执行修改操作
	public function save(){
		//初始化
		// print_r(I());exit;
		if(!$this->_model->create()){
			$this->error($this->_model->getError());
			exit;
		}
		//执行修改 
		if($this->_model->save() >= 0){
			$this->success("修改成功！",U('juese/index'));
		}else{
			$this->error("修改失败");
		}
	}


	//为角色分配权限
	public function quanxianlist(){
		//查找该角色信息
		$juese = $this->_model->where(array('id'=>array('eq',I('id'))))->find();
		//查找所有的节点
		$quanxians = $this->_quanxian->select();

		//获取该角色的权限
		$juese_qianxian = $this->_juese_quanxian->where(array('jid'=>array('eq',$juese['id'])))->select();
		$juese_quanxians = array();
		//遍历重组数组
		foreach($juese_quanxian as $v){
			$juese_quanxians[] = $v['qid'];
		}

		
		

		//向模板分配该用户拥有的权限信息
		$this->assign('juese_quanxians',$juese_quanxians);
		//向模板分配节点信息
		$this->assign('quanxians',$quanxians);
		//向模板分配角色信息ec\\
		$this->assign('juese',$juese);

		//加载模板
		$this->display();


	}


	//为角色添加权限
	public function savequanxian(){
		if(empty($_POST['quanxian'])){
			$this->error("必须选择一个节点！");
		}

		$jid = $_POST['jid'];

		//删除该 角色的 所有信息--避免重复添加
		$this->_juese_quanxian->where(array('jid'=>array('eq',$jid)))->delete();

		//循环添加
		foreach($_POST['quanxian'] as $v){
			$data['qid'] = $v;
			$data['jid'] = $jid;
			//执行添加
			$this->_juese_quanxian->data($data)->add();
		}

		$this->success("添加成功！",U('juese/index'));
	}
}